<?php
namespace addons\cloudstorage\controller;

use addons\cloudstorage\model\CloudStorage;
use think\Addons;
use think\Controller;
use think\Db;
use think\Exception;
use think\facade\Config as FacadeConfig;
use think\facade\Request;
use think\facade\View;

class Config extends Controller
{
    protected $addon = null;
    
    public function initialize()
    {
        parent::initialize();
        $this->addon = Addons::get('cloudstorage');
        View::engine('Think')->config('view_path', $this->addon->getAddonPath() . 'view/');
    }
    
    /**
     * 配置列表
     */
    public function index()
    {
        if (Request::isAjax()) {
            $list = CloudStorage::select();
            $result = ['total' => count($list), 'rows' => $list];
            return json($result);
        }
        
        $types = [
            'aliyun'  => '阿里云OSS',
            'qiniu'   => '七牛云',
            'tencent' => '腾讯云COS'
        ];
        
        $this->assign('types', $types);
        $this->assign('addon', $this->addon->getInfo());
        return View::fetch();
    }
    
    /**
     * 添加配置
     */
    public function add()
    {
        if (Request::isPost()) {
            $data = Request::post();
            
            // 处理配置数据
            $config = [];
            switch ($data['type']) {
                case 'aliyun':
                    $config = [
                        'accessKeyId'     => $data['accessKeyId'],
                        'accessKeySecret' => $data['accessKeySecret'],
                        'endpoint'        => $data['endpoint'],
                        'bucket'          => $data['bucket']
                    ];
                    break;
                case 'qiniu':
                    $config = [
                        'accessKey' => $data['accessKey'],
                        'secretKey' => $data['secretKey'],
                        'bucket'    => $data['bucket'],
                        'zone'      => $data['zone']
                    ];
                    break;
                case 'tencent':
                    $config = [
                        'secretId'  => $data['secretId'],
                        'secretKey' => $data['secretKey'],
                        'region'    => $data['region'],
                        'bucket'    => $data['bucket']
                    ];
                    break;
            }
            
            $data['config'] = $config;
            
            // 如果设为默认，取消其他默认
            if (isset($data['is_default']) && $data['is_default'] == 1) {
                CloudStorage::where('is_default', 1)->update(['is_default' => 0]);
            }
            
            $model = new CloudStorage();
            $result = $model->save($data);
            
            if ($result) {
                $this->success('添加成功', 'index');
            } else {
                $this->error('添加失败');
            }
        }
        
        $type = Request::param('type', 'aliyun');
        $this->assign('type', $type);
        return View::fetch('edit');
    }
    
    /**
     * 编辑配置
     */
    public function edit($id)
    {
        $model = CloudStorage::find($id);
        if (!$model) {
            $this->error('配置不存在');
        }
        
        if (Request::isPost()) {
            $data = Request::post();
            
            // 处理配置数据
            $config = [];
            switch ($data['type']) {
                case 'aliyun':
                    $config = [
                        'accessKeyId'     => $data['accessKeyId'],
                        'accessKeySecret' => $data['accessKeySecret'],
                        'endpoint'        => $data['endpoint'],
                        'bucket'          => $data['bucket']
                    ];
                    break;
                case 'qiniu':
                    $config = [
                        'accessKey' => $data['accessKey'],
                        'secretKey' => $data['secretKey'],
                        'bucket'    => $data['bucket'],
                        'zone'      => $data['zone']
                    ];
                    break;
                case 'tencent':
                    $config = [
                        'secretId'  => $data['secretId'],
                        'secretKey' => $data['secretKey'],
                        'region'    => $data['region'],
                        'bucket'    => $data['bucket']
                    ];
                    break;
            }
            
            $data['config'] = $config;
            
            // 如果设为默认，取消其他默认
            if (isset($data['is_default']) && $data['is_default'] == 1) {
                CloudStorage::where('is_default', 1)->where('id', '<>', $id)->update(['is_default' => 0]);
            }
            
            $result = $model->save($data);
            
            if ($result) {
                $this->success('修改成功', 'index');
            } else {
                $this->error('修改失败');
            }
        }
        
        $this->assign('row', $model);
        return View::fetch('edit');
    }
    
    /**
     * 删除配置
     */
    public function del($id)
    {
        $model = CloudStorage::find($id);
        if (!$model) {
            $this->error('配置不存在');
        }
        
        // 不能删除默认配置
        if ($model->is_default == 1) {
            $this->error('不能删除默认配置');
        }
        
        $result = $model->delete();
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
?>
